package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 沐风 on 2024/4/24 9:43
 */
@Component
@Slf4j
public class OrderTask {
    @Autowired
    private OrderMapper orderMapper;
//    @Scheduled(cron = "0 * * * * ?" )//每分钟执行
    public void processOutOfPayment(){
        log.info("处理支付超时订单：{}", LocalDateTime.now());
        //先查询超时未支付订单
        List<Orders> list = orderMapper.getByStatusAndOrderTimeLT(Orders.UN_PAID,LocalDateTime.now().minusMinutes(15));

        //遍历
        for (Orders orders : list) {
            Orders orders1 = Orders.builder()
                    .id(orders.getId())
                    .status(Orders.CANCELLED)
                    .cancelTime(LocalDateTime.now())
                    .cancelReason("支付超时")
                    .build();
            orderMapper.update(orders1);
        }
    }

    @Scheduled(cron = "0 0 1 * * ? ")//每天一点执行
    public void processOutOfDelivery(){
        log.info("处理一直处于派送中的订单：{}",LocalDateTime.now());
        List<Orders> list = orderMapper.getByStatus(Orders.DELIVERY_IN_PROGRESS);

        for (Orders orders : list) {
            Orders orders1 = Orders.builder()
                    .id(orders.getId())
                    .status(Orders.COMPLETED)
                    .build();
            orderMapper.update(orders1);
        }
    }
}
